1
|
2 |
|
import mixin from './mixin' |
2
|
2 |
|
import { setPageTitle } from './page-title' |
3
|
2 |
|
import { setup as setupRouter } from './router' |
4
|
2 |
|
import {safeString} from './utils' |
5
|
|
|
|
6
|
2 |
|
const install = (Vue, options = {}) => { |
7
|
|
|
// prevent double install |
8
|
|
|
/* istanbul ignore next */ |
9
|
|
|
if (install.installed) return |
10
|
5 |
|
install.installed = true |
11
|
|
|
|
12
|
|
|
// title state |
13
|
5 |
|
const $page = { |
14
|
|
|
title: '', |
15
|
|
|
prefix: safeString(options.prefix), |
16
|
|
|
suffix: safeString(options.suffix) |
17
|
|
|
} |
18
|
|
|
|
19
|
5 |
|
const setTitle = value => { |
20
|
10 |
|
setPageTitle(value, options) |
21
|
10 |
|
$page.title = value |
22
|
|
|
} |
23
|
|
|
|
24
|
5 |
|
const setPrefix = value => { |
25
|
|
|
options.prefix = value |
26
|
|
|
setTitle($page.title) |
27
|
|
|
} |
28
|
|
|
|
29
|
5 |
|
const setSuffix = value => { |
30
|
|
|
options.suffix = value |
31
|
|
|
setTitle($page.title) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// make reactive title properties |
35
|
5 |
|
Vue.util.defineReactive($page, 'title', '') |
36
|
5 |
|
Vue.util.defineReactive($page, 'prefix', '') |
37
|
5 |
|
Vue.util.defineReactive($page, 'suffix', '') |
38
|
|
|
|
39
|
|
|
// add title elements to component context |
40
|
5 |
|
Object.defineProperty(Vue.prototype, '$title', { |
41
|
21 |
|
get: () => $page.title, |
42
|
9 |
|
set: value => setTitle(value) |
43
|
|
|
}) |
44
|
|
|
|
45
|
5 |
|
Object.defineProperty(Vue.prototype, '$titlePrefix', { |
46
|
|
|
get: () => $page.prefix, |
47
|
|
|
set: value => setPrefix(value) |
48
|
|
|
}) |
49
|
|
|
|
50
|
5 |
|
Object.defineProperty(Vue.prototype, '$titleSuffix', { |
51
|
|
|
get: () => $page.suffix, |
52
|
|
|
set: value => setSuffix(value) |
53
|
|
|
}) |
54
|
|
|
|
55
|
|
|
// vue router support |
56
|
5 |
|
if (options.router) { |
57
|
1 |
|
setupRouter(setTitle, setPrefix, setSuffix, options) |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// add global mixin |
61
|
5 |
|
Vue.mixin(mixin) |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
const VuePageTitle = { install } |
65
|
|
|
|
66
|
|
|
export { install } |
67
|
|
|
export default VuePageTitle |
68
|
|
|
|